﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.vs.General;
using com.vs.General.RF;
using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.Task.BL;

namespace com.vs.RF.Web.task
{
    public partial class TaskIDEntryHandling :RFHandlingPage
    {
        private string stTaskID = null;
        TaskPreferenceDO prefDO = null;
        TaskHeaderDO tskHdrDO = null;
        bool bSystemDirect = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            stSourcePage = "task/TaskIDEntry.aspx";
            stTargetPage = "task/PickFromEntry.aspx";
            go();
        }

        protected override void initData()
        {
            prefDO = (TaskPreferenceDO)Session[RFSupport.TASK_PREFERENCE_DO];
            bSystemDirect = StringManager.Equals(prefDO.SYSTEM_DIRECT, "Y");

            //清除Session数据
            Session.Remove(RFSupport.FROM_TASK_DETAIL_DO_LIST);
            Session.Remove(RFSupport.TO_TASK_DETAIL_DO_LIST);
        }

        protected override void parseRequest()
        {
            stTaskID = Request["tbTaskID"];
        }


        protected override void procData()
        {
            TaskManager tskMgr = null;
            List<TaskDetailDO> ltFromTaskDtl = null;
            List<TaskDetailDO> ltToTaskDtl = null;

            try
            {
                this.validateData();
                if (!StringManager.IsEmpty(stError))
                    return;

                tskMgr = new TaskManager();
                //获取拣货任务明细
                ltFromTaskDtl = tskMgr.GetNextFromTaskDetailGroup(rfSess, tskHdrDO.INTERNAL_TASK_NUM, ref stError);
                if (!StringManager.IsEmpty(stError))
                    return;

                if (null != ltFromTaskDtl && ltFromTaskDtl.Count > 0)
                {
                    Session[RFSupport.FROM_TASK_DETAIL_DO_LIST] = ltFromTaskDtl;
                }

                //获取上架部分的任务明细
                ltToTaskDtl = tskMgr.GetNextToTaskDetailGroup(rfSess, tskHdrDO.INTERNAL_TASK_NUM, ref stError);
                if (!StringManager.IsEmpty(stError))
                    return;

                if (null != ltToTaskDtl && ltToTaskDtl.Count > 0)
                {
                    Session[RFSupport.TO_TASK_DETAIL_DO_LIST] = ltToTaskDtl;
                }

                if (null == ltFromTaskDtl && null == ltToTaskDtl)
                {
                    stError = MessageManager.MSG_TSK_002; //"此任务已完成"
                    return;
                }

                if (null == ltFromTaskDtl)
                {
                    stTargetPage = "task/PutToEntry.aspx";
                }

            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }

        protected void validateData()
        {
            TaskManager tskMgr = null;
            TaskExecutionHelper tskHelper = null;

            try
            {
                //todo:此处需要考虑到RF用户的区域权限，不能将超出区域权限的任务赋予用户
                //使用一个单独的方法来获取任务
                tskMgr = new TaskManager();

                if (!bSystemDirect)
                {
                    //如果不是系统分配任务，而且用户没有输入任务号，返回错误
                    if (StringManager.IsEmpty(stTaskID))
                    {
                        stError = MessageManager.MSG_TSK_021; //错误：请输入任务号
                        return;
                    }

                    tskHdrDO = tskMgr.GetAvailableTask(rfSess, stTaskID, prefDO, ref stError);
                    if (!StringManager.IsEmpty(stError))
                        return;

                    if (null == tskHdrDO)
                    {
                        stError = MessageManager.MSG_TSK_009; //任务未找到
                        return;
                    }
                }
                else
                {
                    tskHdrDO = tskMgr.GetNextFromTask(rfSess, prefDO, ref stError);
                    if (!StringManager.IsEmpty(stError))
                        return;

                    if (null == tskHdrDO)
                    {
                        stError = MessageManager.MSG_TSK_022; //没有可以分配的任务
                        return;
                    }
                }

                tskHelper = new TaskExecutionHelper();
                stError = tskHelper.AssignToUser(rfSess, tskHdrDO.INTERNAL_TASK_NUM, rfSess.User);
                if (!StringManager.IsEmpty(stError))
                    return;

            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }

        protected void getAvailableTaskDetails()
        {
           // List<TaskDetailDO> ltTskDtl = null;
        }
    }
}
